# coding:utf-8
import threading
import time
import tkinter as tk

import ttkbootstrap as tkBoot

from util.ui_util import show_center


class LoginMessageBox(tkBoot.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        self.event = threading.Event()
        self.gauge = tkBoot.Floodgauge(self, mask="loading...{}%", bootstyle="primary", length=200)

    def initFrame(self):
        width = int(self.winfo_screenwidth()/6)
        height = int(self.winfo_screenheight()/6)
        self.title("登录中")
        show_center(self, width, height)
        self.resizable(False, False)

        # self.loginInfoText.set("登录中")
        # tkBoot.Label(self, textvariable=self.loginInfoText, font=("微软雅黑", 16)).pack(side=tk.TOP, pady=10)
        self.gauge.pack(side=tk.TOP, pady=15)

        tkBoot.Button(self, width=15, text="取消", command=self.cancel).pack(side=tk.BOTTOM, pady=15)
        self.after(100, self.startPending)
        self.after(1500, self.pageForward)

    def startPending(self):
        self.event.set()
        threading.Thread(target=self.textPending).start()

    def cancel(self):
        self.event.clear()
        self.destroy()

    def pageForward(self):
        self.cancel()
        self.parent.doLogin()

    def textPending(self):
        idx = 0
        while self.event.is_set() and idx < 100:
            idx += 20
            self.gauge.configure(value=idx)
            time.sleep(0.3)
